package com.dreamer.service.lottery.impl;

import com.dreamer.domain.lottery.Gift;
import com.dreamer.domain.lottery.Lottery;
import com.dreamer.domain.lottery.Player;
import com.dreamer.repository.lottery.GiftDao;
import com.dreamer.repository.lottery.LotteryDao;
import com.dreamer.repository.lottery.PlayerDao;
import com.dreamer.repository.mobile.BaseDao;
import com.dreamer.service.lottery.PlayerHandler;
import com.dreamer.service.mobile.impl.BaseHandlerImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ps.mx.otter.exception.ApplicationException;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class PlayerHandlerImpl extends BaseHandlerImpl<Player> implements PlayerHandler {


    @Autowired
    public void setBaseDao(PlayerDao playerDao) {
        this.playerDao = playerDao;
        super.setBaseDao(playerDao);
    }

    @Autowired
    private LotteryDao lotteryDao;

    private PlayerDao playerDao;

    @Autowired
    private GiftDao giftDao;


    /**
     * 购买指定抽奖产品后执行
     *
     * @param uid
     * @param lid
     * @param number 购买数量
     * @return
     */
    @Override
    @Transactional
    public void initPlayer(Integer uid, Integer lid, Integer number) {
        try {
            //创建参与者
            for (int i = 0; i < number; i++) {
                Player player = new Player(lid, null, uid, new Date(), false, false);
                 save(player);
            }
            //修改当前参与抽奖人总数
            Lottery lottery = lotteryDao.get(lid);
            Integer sum = lottery.getSum() + number;
            lottery.setSum(sum);
            lotteryDao.merge(lottery);
            //如果小于参赛总数
            if (sum < lottery.getNumber()) {
                return;
            }
            //计算出满足抽奖的最后一个人
            Integer index = sum/lottery.getNumber();
            Integer lastId = playerDao.findLast(index * lottery.getNumber());
            if(index>lottery.getTimes()){
                //并且激活奖品
                initGift(lid, index, lottery.getNumber());
                //将之前的所有人都设置为可以抽奖
                playerDao.actPlayer(lastId);
                //修改轮数
                lottery.setTimes(index);
                lotteryDao.merge(lottery);
            }

        } catch (Exception e) {
            e.printStackTrace();
            throw new ApplicationException("加入抽奖活动失败");
        }
    }


    /**
     * 初始化抽奖奖品
     */
    @Transactional
    private void initGift(Integer lid, Integer index, Integer number) {
        List<Integer> numbers = new ArrayList();
        //首先查找出所有的用户ID
        List<Integer> pids = playerDao.findByLidAndLimit(lid, (index - 1) * number, number);
        Integer size = pids.size();
        System.out.println("------------");
        //找出奖品数量
        List<Gift> gifts = giftDao.findByLid(lid);
        for (Gift gift : gifts) {
            for (int i = 0; i < gift.getNumber(); i++) {
                Integer rn = new Random().nextInt(size);
                //无限循环 产生随机数
                while (numbers.contains(rn)) {
                    rn = new Random().nextInt(size);
                }
                numbers.add(rn);
                Integer pid = pids.get(rn);
                Player player = playerDao.get(pid);
                player.setGid(gift.getId());
                playerDao.merge(player);
            }
        }
    }




}
